<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2021/8/18
 * Time: 4:18 下午
 */

namespace app\common\cachemodel\live;


use app\common\lib\Util;
use app\common\model\BaseCache;
use Exception;
use XznsTools\cache\CacheManage;

/**
 * Class LiveBroadcastPreview
 */
class LiveBroadcastPreview extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.live_broadcast_preview';

    protected $cacheModel = 'live.broadcast_preview';

    /**
     * 获取预告列表
     * @param int $liveId
     * @return array|mixed
     * @throws Exception
     */
    public function getPreviewList($liveId = 0)
    {
        return $this->getListPage($liveId);
    }

    /**
     * 获取当前直播开播状态
     * @param $user
     * @param $preview
     * @return array
     * @throws Exception
     */
    public function launchStatus($user, $preview)
    {
        $preview['record_id'] = (new LiveRecordingInfo())->getNewestRecord($preview['live_id']);
//        $header = request()->header();
//        $liveNative = 0;//1=>原生 0=>H5
//        if (isset($this->headers['app-type']))
//        {
//            if ($header['app-type'] === "ios" && Util::CheckAppVersion('2.7.3') !== '小')
//            {
//                $liveNative = 1;
//            }
//            if ($header['app-type'] === "android" && Util::CheckAppVersion('2.7.7') !== '小')
//            {
//                $liveNative = 1;
//            }
//        }

        //若直播预约入口关闭则全不推送,若开启
        $live_subscribe_status = 0;
        if (isset($preview['subscribe_status']))
        {
            if ($preview['subscribe_status'])
            {
                if ($user)
                {
                    $subscribeInfo = (new LiveSubscribe())->getUserSubscribeId($user['id'], $preview['id']);
                    if ($subscribeInfo && (int)$subscribeInfo['status'] === 1)
                    {
                        $live_subscribe_status = 1;
                    }
                }
            } else
            {
                $live_subscribe_status = 2;
            }
        }
        $liveTime = date('n月d日 H:i', $preview['live_start_time']);
        $data = [
            'id' => $preview['id'],
            'live_subscribe_status' => $live_subscribe_status,
            'img' => $preview['broadcast_cover_img'],
            'title' => $preview['broadcast_title'],
            'desc' => $preview['broadcast_content'],
            'tips' => "开播时间:{$liveTime}",
            'status' => 1,
            'target' => 'local',
            'link' => "live_web?live_id={$preview['live_id']}&live_record_id=0&live_native=1&need_auth=0",
        ];

        //判断直播是否已开启
        $record = (new LiveRecordingInfo())->getInfoById($preview['record_id']);
        //直播中
        if ($preview['live_start_time'] < time() && isset($record['status']) && (int)$record['status'] === 1)
        {
            if ((int)$record['live_type'] === 3)
            {
                $img = $record['cover_img'];
            } else
            {
                $img = $preview['broadcast_cover_img'];
            }
            //获取直播记录标题内容
            $data = [
                'img' => $img,
                'title' => $record['title'],
                'desc' => $record['content'],
                'tips' => "",
                'status' => 2,
                'target' => 'local',
                'link' => "live_web?live_id={$preview['live_id']}&live_record_id={$preview['record_id']}&live_native=1&need_auth=0",
            ];
        }
        return $data;
    }

    /**
     * 批量获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->whereIn('id', $ids);
            } else
            {
                $this->where('is_show_list', 1);
            }
            return $this->column('*', 'id');
        }
        catch (Exception $e)
        {
            return [];
        }
    }

    /**
     * 获取最新的直播预告(可区分直播间)
     * @param $liveId
     * @return array|bool|mixed|string
     * @throws Exception
     */
    public function getNewestPreview($liveId = 0)
    {
        return CacheManage::getInstance()->setCacheConfig($this->cacheModel, $liveId)->getDataAndSet(function () use ($liveId)
        {
            try
            {
                if ($liveId)
                {
                    $where = [
                        'live_id' => $liveId,
                        'status' => 1,
                    ];
                } else
                {
                    $where = [
                        'status' => 1,
                    ];
                }
                $res = $this->where($where)->order('id', 'DESC')->field('*')->findOrFail()->toArray();
            }
            catch (Exception $e)
            {
                $res = [];
            }
            return $res;
        });
    }
}
